package me.zhengjie;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class BizExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity handleException(HttpServletRequest h, Exception e) throws Exception {
//        Map<String, Object> result = new HashMap<>();
//        result.put("code", -1);
//        result.put("message", e.getMessage());
//
//        StringBuilder sb = new StringBuilder();
//        StackTraceElement[] stackArray = e.getStackTrace();
//        for (int i = 0; i < stackArray.length; i++) {
//            StackTraceElement element = stackArray[i];
//            result.put("trace" + (i + 1), element.toString());
//        }

        e.printStackTrace();

        throw  e;

//        return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
    }
}
